/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mamesoft.namibiamc.beans;

import com.Ostermiller.util.RandPass;
import com.mamesoft.namibiamc.tools.JDBC;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import java.io.Serializable;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author simone
 */
public class RescuePasswordBean implements Serializable{

    private String username;
    private String email;
    private boolean success = false;
    private boolean badData = false;

    /** Creates a new instance of RescuePasswordBean */
    public RescuePasswordBean() {
    }

    public void rescue() {
        try {
            String role = "";
            Connection c = JDBC.connect();
            String q = "SELECT username,email "
                    + "FROM medico "
                    + "WHERE username='" + username + "' AND email='" + email + "'";
            ResultSet set = JDBC.executeQuery(q, c);
            success = set.next();
            badData = !success;

            if (badData) {
                q = "SELECT username,email "
                        + "FROM paziente "
                        + "WHERE username='" + username + "' AND email='" + email + "'";
                set = JDBC.executeQuery(q, c);
                success = set.next();
                badData = !success;
                if (success) {
                    role = "paziente";
                }
            } else {
                role = "medico";
            }
            set.close();
            if (success) {

                RandPass r = new RandPass(RandPass.NUMBERS_AND_LETTERS_ALPHABET);
                String password = r.getPass(8);
                
                sendEmail(email, password); 
                q = "UPDATE " + role + " SET password='" + password + "' WHERE username = '" + username + "'";
                JDBC.executeStatement(q, c);
            }
            c.close();
        } catch (Exception ex) {
            success = false;
            badData = true;
        }
    }

    private void sendEmail(String mail, String pass) throws MessagingException {
        Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        // Get a Properties object
        Properties props = System.getProperties();
        props.setProperty("mail.smtp.host", "smtp.gmail.com");
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.port", "465");
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "false");
        final String user = "namibiamc@gmail.com";
        final String password = "muccamucca";
        Session session = Session.getDefaultInstance(props, new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, password);
            }
        });
        // — Create a new message –
        Message msg = new MimeMessage(session);
        // — Set the FROM and TO fields – 
        msg.setFrom(new InternetAddress(user + ""));
        msg.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("simone.melchiori90@gmail.com", false));
        msg.setSubject("Namibia Medical Control: Recupero Password");
        msg.setText("email:" + mail + "\n\n\n\nCaro/a " + username + "\n \n La tua nuova password è: " + pass + "\n"
                + "è consigliabile cambiare password appena possibile\n"
                + "\n"
                + "Distinti Saluti \n"
                + "Il team di NMC");
        msg.setSentDate(new Date());
        Transport.send(msg);
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the success
     */
    public boolean isSuccess() {
        return success;
    }

    /**
     * @param success the success to set
     */
    public void setSuccess(boolean success) {
        this.success = success;
    }

    /**
     * @return the badData
     */
    public boolean isBadData() {
        return badData;
    }

    /**
     * @param badData the badData to set
     */
    public void setBadData(boolean badData) {
        this.badData = badData;
    }
}
